﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using VIBlend.WPF.Controls;

namespace AnonsaiWPF.CSharp.DataGridViewDemos
{
    [Category("VIBlend.WPF.Examples")]
    public partial class GridLocalization : Window, IWindow
    {
        #region IWindow Members

        public void DetachContent()
        {
            RemoveLogicalChild(Content);
        }

        public Grid Root
        {
            get
            {
                return this.LayoutRoot;
            }
        }
        #endregion

        public void SetTheme()
        {
            dataGrid.FontFamily = new FontFamily("Tahoma");
        }

        public class GermanLocalizationProvider : DataGridLocalization
        {
            public override string GetString(LocalizationNames name)
            {
                switch (name)
                {
                    case LocalizationNames.ContextMenuSortSmallestToLargest:
                        return "Sortieren in aufsteigende Reihenfolge";
                    case LocalizationNames.ContextMenuSortLargestToSmallest:
                        return "Sortieren in absteigende Reihenfolge";
                    case LocalizationNames.ContextMenuRemoveSort:
                        return "Sortieren loschen";
                    case LocalizationNames.ContextMenuFilter:
                        return "Filter";
                    case LocalizationNames.ContextMenuClearFilter:
                        return "Filter loschen";
                    case LocalizationNames.FilterWindowOrRadioButton:
                        return "Oder";
                    case LocalizationNames.FilterWindowOkButton:
                        return "Anwenden";
                    case LocalizationNames.FilterWindowFilterBy:
                        return "Filtern nach: ";
                    case LocalizationNames.FilterWindowCancelButton:
                        return "Stornieren";
                    case LocalizationNames.FilterWindowAndRadioButton:
                        return "Und";
                    case LocalizationNames.FilterOperatorStartsWith:
                        return "Beginnt mit";
                    case LocalizationNames.FilterOperatorSelect:
                        return "Wählen...";
                    case LocalizationNames.FilterOperatorRegularExpression:
                        return "Regulärer Ausdruck";
                    case LocalizationNames.FilterOperatorNotEqual:
                        return "Nicht Gleich";
                    case LocalizationNames.FilterOperatorLessThanOrEqual:
                        return "Weniger als oder gleich";
                    case LocalizationNames.FilterOperatorLessThan:
                        return "Weniger als";
                    case LocalizationNames.FilterOperatorIsNull:
                        return "Ist Null";
                    case LocalizationNames.FilterOperatorIsNotNull:
                        return "Ist Nicht Null";
                    case LocalizationNames.FilterOperatorIsNotEmpty:
                        return "Nicht leer ist";
                    case LocalizationNames.FilterOperatorIsEmpty:
                        return "Leer ist";
                    case LocalizationNames.FilterOperatorGreaterThanOrEqual:
                        return "Größer als oder gleich";
                    case LocalizationNames.FilterOperatorGreaterThan:
                        return "Größer als";
                    case LocalizationNames.FilterOperatorEqual:
                        return "Gleich";
                    case LocalizationNames.FilterOperatorEndsWith:
                        return "Endet mit";
                    case LocalizationNames.FilterOperatorDoesNotContain:
                        return "Nicht enthalten";
                    case LocalizationNames.FilterOperatorContains:
                        return "Enthält";
                    case LocalizationNames.FilterShowRowsWhereText:
                        return "Alle Zeilen, in denen:";
                    case LocalizationNames.FilterValueText:
                        return "Wert:";
                    case LocalizationNames.FilterCriteriaDefinition:
                        return "Filter Definition";
                    case LocalizationNames.FilterItemsAllText:
                        return "Alle auswählen";
                    case LocalizationNames.FilterButtonBasicFilter:
                        return "Basis filter";
                    case LocalizationNames.FilterButtonCustomFilter:
                        return "Komplexe Filter";
                }

                return null;
            }
        }


        public GridLocalization()
        {
            InitializeComponent();

            PrepareGridData();

            dataGrid.ItemsSource = listSaleRecord;
            dataGrid.RowsHierarchy.Visible = false;

            dataGrid.ColumnsHierarchy.AllowDragDrop = false;
            dataGrid.ColumnsHierarchy.AllowResize = false;

            // Enables the Filtering MenuItem in the built-in ContextMenu.
            dataGrid.AllowContextMenuFiltering = true;
        }


        #region Sample Data

        #region Data
        string[] firstNames = new string[] 
                {
                    "Andrew",
                    "Nancy",
                    "Shelley",
                    "Regina",
                    "Yoshi",
                    "Antoni",
                    "Mayumi",
                    "Ian",
                    "Peter",
                    "Lars",
                    "Petra",
                    "Martin",
                    "Sven",
                    "Elio",
                    "Beate",
                    "Cheryl",
                    "Michael",
                    "Guylène"
                };

        string[] lastNames = new string[] 
                {
                    "Fuller",
                    "Davolio",
                    "Burke",
                    "Murphy",
                    "Nagase",
                    "Saavedra",
                    "Ohno",
                    "Devling",
                    "Wilson",
                    "Peterson",
                    "Winkler",
                    "Bein",
                    "Petersen",
                    "Rossi",
                    "Vileid",
                    "Saylor",
                    "Björn",
                    "Nodier"
                };

        static string[,] productNames = new string[,] 
                {
                    {"Black Tea", "1.95"},
                    {"Green Tea", "1.95"},
                    {"Caffè Espresso", "1.45"},
                    {"Doubleshot Espresso", "1.75"},
                    {"Caffè Latte", "2.25"},
                    {"White Chocolate Mocha", "2.35"},
                    {"Caramel Latte", "2.35"},
                    {"Caffè Americano", "1.65"},
                    {"Cappuccino", "2.10"},
                    {"Espresso Truffle", "2.45"},
                    {"Espresso con Panna", "1.81"},
                    {"Peppermint Mocha Twist", "1.99"}
                };

        public class EmployeeSaleRecord
        {
            public EmployeeSaleRecord(string Name, DateTime Date, int ProductId, int Quantity)
            {
                this._name = Name;
                this._date = Date;
                this._productid = ProductId;
                this._quantity = Quantity;
            }

            #region Private Members
            private string _name;
            private DateTime _date;
            private int _productid;
            private int _quantity;
            #endregion

            #region Properties

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }

            public DateTime Date
            {
                get { return _date; }
                set { _date = value; }
            }

            public string Product
            {
                get { return productNames[_productid, 0]; }
            }

            public int Quantity
            {
                get { return _quantity; }
                set { _quantity = value; }
            }

            public double UnitPrice
            {
                get
                {
                    double unitPrice = double.Parse(productNames[_productid, 1]);
                    return unitPrice;
                }
            }

            public double SalesAmount
            {
                get { return (double)_quantity * UnitPrice; }
            }


            #endregion
        }


        #endregion

        public List<EmployeeSaleRecord> listSaleRecord = new List<EmployeeSaleRecord>();

        private void PrepareGridData()
        {
            const int rowsToLoad = 100;
            listSaleRecord.Clear();

            Random rand = new Random();

            for (int i = 0; i < rowsToLoad; i++)
            {
                int productId = rand.Next(0, productNames.Length / 2 - 1);
                int quantity = rand.Next(1, 5);
                listSaleRecord.Add(new EmployeeSaleRecord(
                    string.Format("{0} {1}",
                    firstNames[rand.Next(0, firstNames.Length - 1)],
                    lastNames[rand.Next(0, lastNames.Length - 1)]),
                    DateTime.Now.AddDays(-rand.Next(10, 100)),
                    productId,
                    quantity)
                    );
            }
        }

        #endregion

        // Custom class implements the IValueConverter interface.
        public class ProductIdToNameConverter : IValueConverter
        {
            #region IValueConverter Members

            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if ((int)value >= productNames.Length / 2)
                    return "";

                return productNames[(int)value, 0];
            }

            public object ConvertBack(object value, Type targetType,
                object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }

            #endregion
        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            if (this.dataGrid == null)
                return;

            this.dataGrid.Localization = null;
        }

        private void RadioButton_Checked_1(object sender, RoutedEventArgs e)
        {
            if (this.dataGrid == null)
                return;

            this.dataGrid.Localization = new GermanLocalizationProvider();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            this.listSaleRecord.Clear();
            this.listSaleRecord = null;
            base.OnClosing(e);
            RadioButton1.Checked -= new RoutedEventHandler(RadioButton_Checked);
            RadioButton1.Checked -= new RoutedEventHandler(RadioButton_Checked_1);
            this.dataGrid.Dispose();
           
        }

    } // class CellEditing
}
